package com.example.animation0415;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AttributeActivity extends AppCompatActivity{
    private Button btn_attribute;
    private ObjectAnimator objectAnimator;
    private LinearLayout ll_root;
    private float rotateDu = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attributelayout);

        btn_attribute = findViewById(R.id.btn_attribute);
        ll_root = findViewById(R.id.attri_root);
    }

    //透明度渐变
    public void OnAlpha(View view){
        objectAnimator = ObjectAnimator.ofFloat(btn_attribute,"alpha",1f,0.8f,0.7f,0.2f,0.1f);
        objectAnimator.setDuration(4000);
        objectAnimator.start();
    }

    //缩放
    public void OnScale(View view){
        objectAnimator = ObjectAnimator.ofFloat(btn_attribute,"scaleY",1f,2f,3f,4f,3f,2f,1f);
        objectAnimator.setDuration(4000);
        objectAnimator.start();
    }

    //位移
    public void OnTranslate(View view){
        float width = ll_root.getWidth();
        objectAnimator = ObjectAnimator.ofFloat(btn_attribute,
                "translationX",width/10,width/9,width/4,width/3,width/2,width);
        objectAnimator.setDuration(4000);
        objectAnimator.start();
    }

    //旋转
    public void OnRotate(View view){
        objectAnimator = objectAnimator.ofFloat(btn_attribute,"rotation",
                rotateDu,360);
        objectAnimator.setDuration(2000);
        objectAnimator.setRepeatCount(100);//设置动画重复次数
        objectAnimator.setRepeatMode(ValueAnimator.RESTART); //动画重复模式
        objectAnimator.start();
    }

    //组合动画
    public void OnSet(View view){
        float height = ll_root.getHeight();
        ObjectAnimator objectAnimatorRotate = ObjectAnimator.ofFloat(btn_attribute,
                "rotation",rotateDu,360);
        ObjectAnimator objectAnimatorTr = ObjectAnimator.ofFloat(btn_attribute,
                "translationY",height,height/2,
                height/3,height/4,height/5,height/6);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(4000);
        animatorSet.play(objectAnimatorRotate).with(objectAnimatorTr);
        animatorSet.start();
    }


}
